package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.CheckGroup;
import com.itheima.POJO.Food;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.FoodService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Reference
    private FoodService foodService;
    //分页
    @RequestMapping("/getPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult= foodService.findPage(currentPage,pageSize,queryString);
        return pageResult;
    }
    //新增
    @RequestMapping("/addFood")
    public Result addFood(@RequestBody Food food){
        try{
            foodService.addFood(food);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_Food_FAIL);
        }
        return new Result(true,MessageConstant.ADD_Food_SUCCESS);
    }

    @RequestMapping("/findFoodByid")
    public Result findFoodByid(Integer id){
       Result  result=foodService.findFoodByid(id);
        return result;
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Food food){
        try {
            foodService.updateFood(food);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        }
        return new Result(true,"编辑成功");
    }
    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer foodId) {
        Result result = foodService.delete(foodId);
        return result;
    }
}
